module.exports = {
	default: function(context: any) {

		const buildHints = async (prefix: string) =>{
			const {tags, keepText } = await context.postMessage({
				command: 'getTags',
			});
			let hints = [];
			prefix = prefix.replace(/、/g,'\\') //不生效是，文件编码是GBk，改为UTF-8即可 .js 和typescript replace 只替换第一个
			console.log('--0-- prefix :'+ prefix)
			for (let i = 0; i < tags.length; i++) {
				const tag = tags[i];
				if (prefix === '#' || tag.title.startsWith(prefix) || tag.fulltitle.startsWith(prefix)) {
					console.log('prefix :'+ prefix +',title:'+tag.title+',full:'+tag.fulltitle)
					hints.push({
						text: '#' + tag.fulltitle,//todo 
						displayText: tag.fulltitle,
						hint: async (cm, data, completion) => {
							context.postMessage({
								command: 'setTag',
								tag: tag,
							});
							if (!!keepText) {
								cm.replaceRange(tag.fulltitle, completion.from || data.from, cm.getCursor(), "complete");
							} else {
								const from = completion.from || data.from;
								from.ch -= 1;
								cm.replaceRange('', from, cm.getCursor(), "complete");
							}
						}
					});
				}
			}
			if (prefix !== '#') {
				const tagExists = tags.findIndex(t => t.title === prefix) > 0;
				console.log('--2-- prefix :'+ prefix +',title:'+tagExists)
				if (!tagExists) {
					hints.push({
						text: `Create new tag: ${prefix}`,
						hint: (cm, data, completion) => {
							context.postMessage({
								command: 'newTag',
								name: prefix,
							});
							if (!keepText) {
								const from = completion.from || data.from;
								from.ch -= 1;
								cm.replaceRange('', from, cm.getCursor(), "complete");
							}
						}
					});
				}
			}
			return hints;
		}

		const plugin = function(CodeMirror) {

			CodeMirror.defineOption('inlineTags', false, function(cm, value, prev) {
				if (!value) return;
				cm.on('inputRead', async function (cm1, change) {
					if (!cm1.state.completionActive && change.text[0] === '#') {
						const content = await cm1.getValue().split('\n');
						let count=0;
						for(let i=0;i<content.length;i++){
							if(content[i].startsWith('```')){
								count++;
								if(i==change.from.line){
									return;
								}
							}
							if(i==change.from.line && count%2){
								return;
							}
						}
						const start = {line: change.from.line, ch: change.from.ch + 1};
						const hint = function(cm, callback) {
							const cursor = cm.getCursor();
							const token = cm.getRange(start, cursor);
							// console.log('token :'+ token)
							buildHints(token).then(hints => {
								callback({
									list: hints,
									from: {line: change.from.line, ch: change.from.ch + 1},
									to: {line: change.to.line, ch: change.to.ch + 1},
								});
							});
						};

						setTimeout(function () {
							CodeMirror.showHint(cm, hint, {
								completeSingle: false,
								closeOnUnfocus: true,
								async: true,
							});
						}, 10);
					}
				});
			});
		};

		return {
			plugin: plugin,
			codeMirrorResources: [
			    'addon/hint/show-hint',
			    ],
			codeMirrorOptions: {
    			'inlineTags': true,
			},
			assets: function() {
			    return [
			        { name: './show-hint.css'},
			    ]
			}
        }
    }
}